package com.version1_3.dao.implement;

import com.SQLGroup.SQLAccess;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.version1_3.dao.SharedMethodDao1_3;
import com.version1_3.dao.SubscriDao1_3;
import com.version1_3.service.SubscriService1_3;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kuibu on 15/12/25.
 */
@Repository
public class SubscriDaoImpl13 implements SubscriDao1_3 {
    private static final Logger LOGGER = Logger.getLogger(SubscriDaoImpl13.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private SharedMethodDao1_3 sharedMethodDao13;

    /**
     * 更新用户关注信息
     */
    @Override
    public boolean changeUserFocus(JSONArray updateFocusJsonArray, int userId) {
        String userFocusStr = sharedMethodDao13.getUserFocusStr(userId);

        // 如果在数据库没有查找到用户关注信息
        if (userFocusStr == null) {
            LOGGER.error("在数据库中未查找到用户关注分类信息,userId: " + userId);
            return false;
        }

        JSONArray jsonArray = JSON.parseArray(userFocusStr);

        // 更新用户关注信息
        for (int i = 0; i < updateFocusJsonArray.size(); i++) {
            JSONArray firstFocusJsonArray = updateFocusJsonArray.getJSONArray(i);

            // 用户端更新关注分类逻辑是,把用户关注分类信息都发过来,直接更新即可,2016/03/04 reeves
            jsonArray.set(i, firstFocusJsonArray);

            /*if (!firstFocusJsonArray.isEmpty()) {
                jsonArray.set(i, firstFocusJsonArray);
            }*/
        }

        sharedMethodDao13.updateTypeSubscriNumber(updateFocusJsonArray, "ios", userId); // 更改分类订阅数

        return sharedMethodDao13.updateUserFocus(jsonArray.toString(), userId);
    }

    /**
     * 订阅单独的三级分类
     */
    @Override
    public boolean subscriThirdType(int userId, int firstType, int thirdTypeId) {
        String userFocusListStr = "";

        /**
         * 1. 根据userId获得用户关注字符串
         */
        try {
            userFocusListStr = jdbcTemplate.queryForObject(SQLAccess.SelectUserFocusInfoVersion1_3, new Object[]{userId}, String.class);
        } catch (EmptyResultDataAccessException exception) {
            LOGGER.error("订阅单独的三级分类出错,根据userId找不到用户,userId: " + userId);
            return false;
        }

        JSONArray jsonArray = JSONArray.parseArray(userFocusListStr);
        JSONArray firstFocusJsonArray = jsonArray.getJSONArray(firstType - 2);
        firstFocusJsonArray.add(thirdTypeId);

        /**
         * 2. 把分类添加到用户的关注列表里面
         */
        sharedMethodDao13.updateUserFocus(jsonArray.toString(), userId);
        return true;
    }

    /**
     * 取消订阅单独的三级分类
     */
    @Override
    public boolean removeSubscri(int userId, int firstType, int thirdTypeId) {
        String userFocusListStr = "";

        /**
         * 1. 根据userId获得用户关注字符串
         */
        try {
            userFocusListStr = jdbcTemplate.queryForObject(SQLAccess.SelectUserFocusInfoVersion1_3, new Object[]{userId}, String.class);
        } catch (EmptyResultDataAccessException exception) {
            LOGGER.error("订阅单独的三级分类出错,根据userId找不到用户,userId: " + userId);
            return false;
        }

        JSONArray jsonArray = JSONArray.parseArray(userFocusListStr);
        JSONArray firstFocusJsonArray = jsonArray.getJSONArray(firstType - 2);
        firstFocusJsonArray.remove(firstFocusJsonArray.indexOf(thirdTypeId));

        /**
         * 2. 把分类添加到用户的关注列表里面
         */
        sharedMethodDao13.updateUserFocus(jsonArray.toString(), userId);
        return true;
    }
}
